package fr.charlott.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import fr.charlott.business.dto.ClienteDTO;
import fr.charlott.business.service.ClienteService;

@Component("listeClienteControler")
@Scope("view")
public class ListeClienteControler extends CharlottControler {

	private static final long serialVersionUID = 2465999191538831539L;

	private List<ClienteDTO> clientes = new ArrayList<ClienteDTO>();

	private ClienteDTO selectedCliente;

	private ClienteService clienteService;
	
	public ListeClienteControler() {
		super();
	}

	@Autowired(required = true)
	public ListeClienteControler(ClienteService clienteService) {
		this();
		this.clienteService = clienteService;
		// Initialisation des différentes Clientes
		clientes = clienteService.chargerToutesLesClientes();
	}

	// actions
	public String modifierCliente() {
		// Mettre la Cliente selectionnee dans la session pour le passage par redirection
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		externalContext.getSessionMap().put(NouvelleClienteControler.EDITED_CLIENTE, selectedCliente);

		return "nouvelleCliente";
	}
	
	public String supprimerCliente() {
		clienteService.supprimerCliente(selectedCliente);
		return "listeClientes";
	}

	// getters - setters

	public List<ClienteDTO> getClientes() {
		return clientes;
	}

	public ClienteDTO getSelectedCliente() {
		return selectedCliente;
	}

	public void setSelectedCliente(ClienteDTO selectedCliente) {
		this.selectedCliente = selectedCliente;
	}
}
